﻿#include "userinfo.h"
#include <QDir>
#include <QCoreApplication>

UserInfo::UserInfo(QObject *parent) : QObject(parent)
{
    dirpath = QCoreApplication::applicationDirPath() + "/UserInfo";
    QDir dir(dirpath);
    if (!dir.exists())
    {
        dir.mkdir(dirpath);
    }
}

void UserInfo::AddUser(const QString &account, const QPixmap &photo)
{
    QDir dir(dirpath);
    if (!dir.exists(account))
    {
        dir.mkdir(account);
    }

    QFile file(dirpath + "/" + account + "/photo.png");
    if (file.exists())
    {
        file.remove();
    }
    file.open(QIODevice::WriteOnly);
    photo.save(&file, "PNG");
    file.close();
}

QPixmap UserInfo::GetPhoto(const QString& account)
{
    QDir dir(dirpath);
    if (!dir.exists(account))
    {
        return QPixmap();
    }
    QFile file(dirpath + "/" + account + "/photo.png");
    if (!file.exists())
    {
        return QPixmap();
    }
    file.open(QIODevice::ReadOnly);
    return QPixmap::fromImage(QImage::fromData(file.readAll()));
}
